function isEmpty(value){//验证是否为空
	if(value != '' || value.trim().length != 0)
		return false;
	else
		return true;
}

function validation_length(value, min, max){//验证数据成都是否符合，汉字算2个字符
	var length_count = 0;
	value = value.trim();
	for(var i = 0; i < value.length; i++){
		if((value.charCodeAt(i) < 0) || (value.charCodeAt(i) > 255)){
			length_count +=2;
		}else{
			length_count++;
		}
	}
	if(length_count < min || length_count > max){
		return true
	}else{
		return false;
	}
}
/*验证密码和确认密码是否一致*/
function equal(value1, value2){
	if(value1 != value2)
		return true;
}
/*验证邮箱格式*/
function validation_email(value){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(!filter.test(value))
		return true;
}
/*验证手机号码格式*/
function validation_number(value){
	if(!(/^1[34578]\d{9}$/.test(value)))
		return true;
}
var input = document.getElementsByTagName('input'),
		name1 = input[0],
		password = input[1],
		confirm_password = input[2],
		email = input[3],
		mobile = input[4];
var arr = [
	{dom: name1,             field:'名称', 		prompt: '必填，长度为4-16个字符', 		flag: false,	warning: '长度为4-16个字符', 					success: '名称可用', 			rules: function(){return validation_length(this.dom.value, 4, 16)}},
	{dom: password, 				field:'密码', 		prompt: '必填，长度为8-16个字符', 		flag: false,	warning: '长度为8-16个字符', 					success: '密码可用', 			rules: function(){return validation_length(this.dom.value, 8, 16)}},
	{dom: confirm_password, field:'确认密码', prompt: '必填，和密码保持一致',				flag: false,  warning: '密码和确认密码输入不一致', 	success: '密码输入一致', 	rules: function(){return equal(this.dom.value,password.value.trim())}},
	{dom: email, 						field:'邮箱', 		prompt: '必填，请输入正确的邮箱格式', flag: false,	warning: '邮箱格式不正确', 						success: '邮箱可用', 			rules: function(){return validation_email(this.dom.value)}},
	{dom: mobile, 					field:'手机号码', prompt: '必填，请输入正确的手机号', 	flag: false,	warning: '手机号码不正确', 						success: '手机号码可用', 	rules: function(){return validation_number(this.dom.value)}}
	
];

function focus(obj){
	obj.dom.onfocus = function(){
		this.style.border = '1px solid #51A6EB';
		this.parentNode.children[1].innerHTML = obj.prompt;
		this.parentNode.children[1].style.color = '#51A6EB';
	};
}
function blur(obj){
	obj.dom.onblur = function(){
		if(isEmpty(this.value)){
			this.style.border = '1px solid red';
			this.parentNode.children[1].innerHTML = obj.field + '不能为空';
			this.parentNode.children[1].style.color = 'red';
			return;
		}
		if(obj.rules()){
			this.style.border = '1px solid red';
			this.parentNode.children[1].innerHTML = obj.warning;
			this.parentNode.children[1].style.color = 'red';
		}else{
			this.style.border = '1px solid green';
			this.parentNode.children[1].innerHTML = obj.success;
			this.parentNode.children[1].style.color = 'green';
			obj.flag = true;
		}
	};
}

for(var i in arr){//给各字段绑定事件
	focus(arr[i]);
	blur(arr[i]);
}

document.getElementById('tijiao').onclick = function(){//验证是否全部输入正确，全部正确提交，有一个错误就不提交，并显示出来
	for(var i in arr){
		arr[i].dom.focus();
		arr[i].dom.blur();
	}
	for(var i in arr){
		if(!arr[i].flag){
			alert('表单字段填写有误');
			return false;
		}
	}
	
	alert('提交成功');
	
}